layui.use(['form', 'element'], function () {
    var $ = layui.$;
    var element = layui.element;

    var roleId = $("#role_id").attr('data-id')
    $.ajax({
        url: ctxPath + "/admin/roles/" + roleId + "/menus/tree",
        method: 'GET',
        success: function (data) {
            var menus = data.data[0].children
            var html = []
            $.each(menus, function (index, elem) {
                if(elem.checked) {
                    html.push('<li class="layui-nav-item">')
                    html.push(` <a class="" href="javascript:;">${elem.name}</a>`)
                    html.push(function () {
                        var dd = new Array()
                        if (elem.children && elem.children.length > 0) {
                            dd.push('<dl class="layui-nav-child">')
                            $.each(elem.children, function (i, e) {
                               if(e.checked) {
                                   dd.push(`<dd><a href="javascript:;" link="${ctxPath + e.link}">${e.name}</a></dd>`)
                               }
                            })
                            dd.push('</dl>')
                        }
                        return dd.join("")
                    }())
                    html.push("</li>")
                }
            })
            $("ul[lay-filter=test]").html(html.join(''))

            element.render('nav', 'test')
            $("a[link]").on('click', function () {
                var thiz = $(this);
                var link = thiz.attr("link");
                $("#content").attr("src", link);
            })
        }
    });


})
